package com.sdtele.tta.operation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.Team;
import com.sdtele.tta.models.User;
import com.sdtele.tta.utils.TeamUtils;

public class OperationGetSubTeams extends Operation {

	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");

		long teamId = jsonRequest.getJsonObject().get("teamId").getAsLong();
		Team team = Dac.getInstance().getTeam(teamId);
		Set<Long> teamIdSet = new HashSet<Long>();
		teamIdSet.add(teamId);
		TeamUtils.teamAuthority(teamId, teamIdSet);
		List<Map<String, Object>> subTeams = new ArrayList<Map<String, Object>>();		
		for (long id:teamIdSet) {
			Map<String, Object> tMap = new HashMap<String, Object>();
			tMap.put("teamId", Dac.getInstance().getTeam(id).getId());
			tMap.put("teamName", Dac.getInstance().getTeam(id).getTeamName());
			long upTeamId=Dac.getInstance().getTeam(id).getUpTeamId();
			tMap.put("upTeamId",upTeamId);
			if (Dac.getInstance().getTeam(upTeamId)==null)
				tMap.put("upTeamName", "无");
			else {
				String upTeamName = Dac.getInstance().getTeam(upTeamId).getTeamName();
				tMap.put("upTeamName", upTeamName);
			}
			subTeams.add(tMap);
		}
		if (curUser != null) {
			jres.setStatus(0);
			jres.setEntity(subTeams);
		} else {
			jres.setStatus(1);
			jres.setMessage("用户未登录");
		}
		return jres;
	}
}
